#!/bin/sh

#
# Upload ipa file to fir.im
#
# Syntax: upload_fir.sh {my-application.ipa}
#

apk=$1
changelog=$2
version=$3

if [ -z "$apk" ]
then
	echo "Syntax: upload_fir.sh {my-application.ipa}"
	exit 1
fi

if [-z "$version"]
then
	version="24"
fi

USER_TOKEN="f72f5850814111e4975f2f22a4e289864a529f66"
APP_ID="com.qdaily.ui"
TYPE="android"

echo "getting token"

INFO=`curl http://fir.im/api/v2/app/info/${APP_ID}\?token=${USER_TOKEN}\&type=${TYPE} 2>/dev/null`
echo INFO ${INFO}

KEY=$(echo ${INFO} | grep "pkg.*url" -o | grep "key.*$" -o | awk -F '"' '{print $3;}')
TOKEN=$(echo ${INFO} | grep "pkg.*url" -o | grep "token.*$" -o | awk -F '"' '{print $3;}')

echo key ${KEY}
echo token ${TOKEN}

echo "uploading"
APP_INFO=`curl -# -F file=@${apk} -F "key=${KEY}" -F "token=${TOKEN}" http://up.qiniu.com`

if [ $? != 0 ]
then
	echo "upload error"
	exit 1
fi

APPOID=`echo ${APP_INFO} | grep "appOid.*," -o | awk -F '"' '{print $3;}'`

echo ${APP_INFO}
echo ${APPOID}

curl -X PUT -d changelog=${changelog}\&version=${version}\&versionShort="1.9.7" http://fir.im/api/v2/app/${APPOID}?token=${USER_TOKEN}

